package com.china08.yxyapi.api.manage.tec.office.attendance;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.attendance.YxyTeaAttendance;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.attendance.YxyTeaAttendanceRepository;
import com.china08.yxyapi.repository.mg.attendance.YxyTeaAttendanceStatisticsRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

/**
 * 教师考勤--我的考勤--打卡次数
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class AttendanceYxyTeaAttendanceCrtCountController extends YxyApiBaseController {

    @Autowired
    private YxyTeaAttendanceRepository yxyTeaAttendanceRepository;

    @Autowired
    private YxyTeaAttendanceStatisticsRepository yxyTeaAttendanceStatisticsRepository;

    @RequestMapping(value = "/api_yw/manage/tec/office/attendance/TeaAttendanceCrtCount", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody(required = false) ReqModel reqModel, RespModel respModel) throws ParseException {

        YxyProfile yxyProfile = getProfile(request, response);
        Date signTime = reqModel.signTime;
        String teacherId = getProfile(request, response).getId();
        String schoolId = yxyProfile.getSchoolId();
        Integer date2 = reqModel.date2;
        if (date2 == null) {
            Assert.isTrue(false, "选择月份不能为空！");
        }

        /* 今日打卡次数 */
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        Integer dayOfDate = Integer.valueOf(dateFormat.format(date));
        Integer count = yxyTeaAttendanceRepository.countByDayOfDateAndTeacherIdAndSchoolId(dayOfDate, teacherId,
                schoolId);
        RespModel model = new RespModel();
        model.counts = count;

        /* 本月打卡次数 */
        // 当前月份+1
        SimpleDateFormat dfDateFormat = new SimpleDateFormat("yyyyMM");
        Date date04 = dfDateFormat.parse(date2 + "");
        Calendar calender = Calendar.getInstance();
        calender.setTime(date04);
        calender.add(Calendar.MONTH, 1);
        Date date05 = calender.getTime();
        Integer intDate05 = Integer.parseInt(dfDateFormat.format(date05));

        // 正常打卡，迟到，早退（1、2、3）
        List<Integer> singnLst = new ArrayList<Integer>();
        singnLst.add(1);
        singnLst.add(2);
        singnLst.add(3);

        // 分别统计本月每次打卡次数，再相加
        Integer singn1 = yxyTeaAttendanceStatisticsRepository
                .countByTeacherIdAndSchoolIdAndSignStatus1InAndDayOfDateBetween(teacherId, schoolId, singnLst,
                        date2 * 100, intDate05 * 100);
        Integer singn2 = yxyTeaAttendanceStatisticsRepository
                .countByTeacherIdAndSchoolIdAndSignStatus2InAndDayOfDateBetween(teacherId, schoolId, singnLst,
                        date2 * 100, intDate05 * 100);
        Integer singn3 = yxyTeaAttendanceStatisticsRepository
                .countByTeacherIdAndSchoolIdAndSignStatus3InAndDayOfDateBetween(teacherId, schoolId, singnLst,
                        date2 * 100, intDate05 * 100);
        Integer singn4 = yxyTeaAttendanceStatisticsRepository
                .countByTeacherIdAndSchoolIdAndSignStatus4InAndDayOfDateBetween(teacherId, schoolId, singnLst,
                        date2 * 100, intDate05 * 100);
        Integer monthCounts = singn1 + singn2 + singn3 + singn4;
        model.monthCounts = monthCounts;

        /* 本月异常打卡次数 */
        // 未打卡和正常打卡（0、1）
        List<Integer> singnIntegers = new ArrayList<Integer>();
        singnIntegers.add(0);
        singnIntegers.add(1);

        // 分别统计本月每次异常打卡次数，再相加
        Integer error1 = yxyTeaAttendanceStatisticsRepository
                .countByTeacherIdAndSchoolIdAndSignStatus1NotInAndDayOfDateBetween(teacherId, schoolId, singnIntegers,
                        date2 * 100, intDate05 * 100);
        Integer error2 = yxyTeaAttendanceStatisticsRepository
                .countByTeacherIdAndSchoolIdAndSignStatus2NotInAndDayOfDateBetween(teacherId, schoolId, singnIntegers,
                        date2 * 100, intDate05 * 100);
        Integer error3 = yxyTeaAttendanceStatisticsRepository
                .countByTeacherIdAndSchoolIdAndSignStatus3NotInAndDayOfDateBetween(teacherId, schoolId, singnIntegers,
                        date2 * 100, intDate05 * 100);
        Integer error4 = yxyTeaAttendanceStatisticsRepository
                .countByTeacherIdAndSchoolIdAndSignStatus4NotInAndDayOfDateBetween(teacherId, schoolId, singnIntegers,
                        date2 * 100, intDate05 * 100);
        model.errorCounts = error1 + error2 + error3 + error4;

        /* 最近一次打卡时间 */

        YxyTeaAttendance yxyTeaAttendance = yxyTeaAttendanceRepository
                .findTop1ByTeacherIdAndSchoolIdOrderBySignTimeDesc(teacherId, schoolId, signTime);
        if (yxyTeaAttendance != null) {
            model.latelySignTime = yxyTeaAttendance.getSignTime();
            model.state = 0;
        }

        return model;

    }

    protected static class ReqModel extends YxyApiReqModel {

        // 打卡时间（例：201705）
        public Integer date2;

        // 签到时间
        public Date signTime;
    }

    protected static class RespModel extends YxyApiRespModel {

        // 今日打卡次数
        public Integer counts;

        // 本月打卡次数
        public Integer monthCounts;

        // 本月异常打卡次数
        public Integer errorCounts;

        // 最近一次打卡时间
        public Date latelySignTime;

        // 是否显示时间（状态1:不显示，0：显示）
        public Integer state = 1;

    }
}
